<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<link href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
		<link rel="stylesheet" href="../css/app.css" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">通知</h1>
		</header>
		<div class="mui-content mui-scroll-wrapper">
			<div class="mui-scroll">
				<ul class="mui-table-view" id="wd_msg_ul">
				</ul>
				<ul class="mui-table-view" id="yd_msg_ul">
				</ul>
			</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script src="../js/jquery.min.js"></script>
		<script src="http://cdn.bootcss.com/moment.js/2.11.2/moment.min.js"></script>
		<script src="http://cdn.bootcss.com/moment.js/2.11.2/locale/zh-cn.js"></script>
		<script src="../js/common.js"></script>
		<script type="text/javascript" charset="utf-8">
			var index;
			mui.init();
			mui.plusReady(function(){
				//获得主页面webview引用；
				index = plus.webview.currentWebview().opener();
				if(isLogin()) {
					mui.fire(index, "hideCreateTopic");
				}
				initData();
			});
			function initData() {
				mui.ajax({
					type: "get",
					url: _baseUrl + "/api/notification",
					data: {
						token: plus.storage.getItem(_user_token)
					},
					async: true,
					dataType: "json",
					success: function(data) {
						var oldMessages = data.detail.oldMessages;
						var notifications = data.detail.notifications;
						var wd_msg_ul = $("#wd_msg_ul");
						wd_msg_ul.html('<li class="mui-table-view-divider">未读消息</li>');
						mui.each(notifications, function(index, _data){
							var topic_message; 
							if(_data.source == 'topic') {
								topic_message = '<a href="javascript:topic_detail(\''+_data.target_id+'\')">'+_data.message+'</a>'
							} else {
								topic_message = _data.message;
							}
							var html = '<li class="mui-table-view-cell">' + 
								'<div>' +
								   	_data.nickname +
								    moment(_data.in_time, 'YYYY-MM-DD HH:mm:ss').fromNow() + ' ' + _data.action +
								    topic_message +
								'</div>' +
							'</li>';
							wd_msg_ul.append(html);
						});
						var yd_msg_ul = $("#yd_msg_ul");
						yd_msg_ul.html('<li class="mui-table-view-divider">已读消息</li>');
						mui.each(oldMessages, function(index, _data){
							var topic_message; 
							if(_data.source == 'topic') {
								topic_message = '<a href="javascript:topic_detail(\''+_data.target_id+'\')">'+_data.message+'</a>'
							} else {
								topic_message = _data.message;
							}
							var html = '<li class="mui-table-view-cell">' + 
								'<div>' +
								   	_data.nickname +
								    moment(_data.in_time, 'YYYY-MM-DD HH:mm:ss').fromNow() + ' ' + _data.action +
								    topic_message +
								'</div>' +
							'</li>';
							yd_msg_ul.append(html);
						});
					}
				});
			}
		</script>
	</body>

</html>